package com.roc.web.action.merinfo;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.roc.bean.JsonBean;
import com.roc.bean.MerinfoSearchBean;
import com.roc.page.BaseAction;
import com.roc.pojo.Operators;
import com.roc.pojo.susesMerinfo;
import com.roc.service.IMerchantServices;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;

import net.sf.json.JSON;
import net.sf.json.JSONObject;

/**
*@auhor   zbl
*@date    2016年12月14日 下午1:10:11
*@desc    说明
**/
@Controller
@Namespace("/merinfoAjax")
@SuppressWarnings("all")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class MerinfoAjaxAction extends BaseAction{
	private static Logger logger = Logger.getLogger(MerinfoAjaxAction.class);
	private String json;
	
	private String chooseType;//类型
	private String params;//获取页面参数
	private String merNo;//商户号
	private String newBankAccount;//结算卡号
	private String merName;//商户名称
	private String contactPhoneNo;
	@Autowired
	private IMerchantServices merchantService;
	

	/**
	 * 查询法人身份证、营业执照、结算卡、手机号 已经进件的次数
	 * */
	@Action(value = "getCount",results={
			 @Result(name=SUCCESS,type="json",params={"json","${json}"})
	})
	public String getCount(){
		try {
			Operators op = (Operators) getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			JsonBean jsonBean=merchantService.queryCount(merNo,chooseType,params,op);
			
			json=JSONObject.fromObject(jsonBean).toString();
			logger.info("=====审核商户["+merNo+"]查询统计["+chooseType+"]身份证、营业执照、结算卡号使用次数返回"+json+"====");
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
		}
		return SUCCESS;
	}
	
	/**
	 * 查询结算卡已经进件的次数
	 * */
	@Action(value = "getCountByAccountNo")
	public void getCountByAccountNo(){
		PrintWriter pw = null;
		try {
			getResponse().setCharacterEncoding("utf-8");
			pw = getResponse().getWriter();
			JsonBean jsonBean=merchantService.queryCountByAccountNo(newBankAccount);
			json=JSONObject.fromObject(jsonBean).toString();
			pw.write(json);
			logger.info("=====审核商户["+merNo+"]查询统计["+chooseType+"]身份证、营业执照、结算卡号使用次数返回"+json+"====");
		} catch (Exception e) {
			JsonBean js=new JsonBean();
			js.setCode("0000");
			js.setCodeDesc("");
			json=JSONObject.fromObject(js).toString();
			pw.write(json.toString());
		}finally{
			if(pw != null){
				pw.close();
			}
		}
	}
	
	/**
	 * 查询商户名称已经注册的次数(临时表)
	 * ******返回json 类型报错，所以返回流
	 * */
	@Action(value = "getMchtNameCount")
	public void getMchtNameCount(){
		PrintWriter pw = null;
		try {
			getResponse().setCharacterEncoding("utf-8");
			pw = getResponse().getWriter();
			JsonBean jsonBean=merchantService.queryMchtCountByName(merNo,merName,false);
			json=JSONObject.fromObject(jsonBean).toString();
			logger.info("=====查询商户名称已经注册的次数返回json-->["+json+"]====");
			pw.write(json);
		} catch (Exception e) {
			JsonBean js=new JsonBean();
			js.setCode("0000");
			js.setCodeDesc("");
			json=JSONObject.fromObject(js).toString();
			pw.write(json.toString());
		}finally{
			if(pw != null){
				pw.close();
			}
		}
	}
	 
	/**
     * 查询商户手机号已经注册的次数(真实时表)
     * ******返回json 类型报错，所以返回流
     * */
    @Action(value = "getMchtNameRealCountPhone")
    public void getMchtNameRealCountPhone(){
            PrintWriter pw = null;
            try {
                    getResponse().setCharacterEncoding("utf-8");
                    pw = getResponse().getWriter();
                    JsonBean jsonBean=merchantService.queryMchtCountByPhone(contactPhoneNo,true);
                    json=JSONObject.fromObject(jsonBean).toString();
                    logger.info("=====查询商户名称已经注册的次数返回json-->["+json+"]====");
                    pw.write(json);
                                            
            } catch (Exception e) {
                    JsonBean js=new JsonBean();
                    js.setCode("0000");
                    js.setCodeDesc("");
                    json=JSONObject.fromObject(js).toString();
                    pw.write(json.toString());
            }finally{
                    if(pw != null){
                            pw.close();
                    }
            }
    }
	
	
	/**
     * 查询商户名称已经注册的次数(真实时表)
     * ******返回json 类型报错，所以返回流
     * */
@Action(value = "getMchtNameRealCount")
    public void getMchtNameRealCount(){
            PrintWriter pw = null;
            try {
                    getResponse().setCharacterEncoding("utf-8");
                    pw = getResponse().getWriter();
                    JsonBean jsonBean=merchantService.queryMchtCountByName(null,merName,true);
                    json=JSONObject.fromObject(jsonBean).toString();
                    logger.info("=====查询商户名称已经注册的次数返回json-->["+json+"]====");
                    pw.write(json);
                                            
            } catch (Exception e) {
                    JsonBean js=new JsonBean();
                    js.setCode("0000");
                    js.setCodeDesc("");
                    json=JSONObject.fromObject(js).toString();
                    pw.write(json.toString());
            }finally{
                    if(pw != null){
                            pw.close();
                    }
            }
    }
        /**
        * 查询商户添加了几台终端
        * ******返回json 类型报错，所以返回流
        * */
        @Action(value = "getTermCountByMchtNo")
        public void getTermCountByMchtNo(){
            PrintWriter pw = null;
            try {
                    getResponse().setCharacterEncoding("utf-8");
                    pw = getResponse().getWriter();
                    JsonBean jsonBean=merchantService.getMchtNameRealCount(merNo);
                    json=JSONObject.fromObject(jsonBean).toString();
                    logger.info("=====查询商户号对应的终端数量返回json-->["+json+"]====");
                    pw.write(json);
                                            
            } catch (Exception e) {
                    JsonBean js=new JsonBean();
                    js.setCode("0000");
                    js.setCodeDesc("");
                    json=JSONObject.fromObject(js).toString();
                    pw.write(json.toString());
            }finally{
                    if(pw != null){
                            pw.close();
                    }
            }
        }
    
    public String getNewBankAccount() {
		return newBankAccount;
	}

	public void setNewBankAccount(String newBankAccount) {
		this.newBankAccount = newBankAccount;
	}
	
	
	public String getMerNo() {
		return merNo;
	}
	public void setMerNo(String merNo) {
		this.merNo = merNo;
	}



	public String getJson() {
		return json;
	}



	public void setJson(String json) {
		this.json = json;
	}




	public String getChooseType() {
		return chooseType;
	}




	public void setChooseType(String chooseType) {
		this.chooseType = chooseType;
	}

	public String getMerName() {
		return merName;
	}

	public void setMerName(String merName) {
		this.merName = merName;
	}

	public String getContactPhoneNo() {
		return contactPhoneNo;
	}

	public void setContactPhoneNo(String contactPhoneNo) {
		this.contactPhoneNo = contactPhoneNo;
	}

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}
}
